1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 namespace WarehouseManagementSystem
11 {
12 public partial class frmChangePassword : Form
13 {
14
15 SqlConnection con = null;
16 SqlCommand cmd = null;
17 ConnectionString cs = new ConnectionString();
18 public frmChangePassword()
19 {
20 InitializeComponent();
21 }
22
23 private void Button1_Click(object sender, EventArgs e)
24 {
25 try
26 {
27 int RowsAffected = 0;
28 if ((txtUserName.Text.Trim().Length == 0))
29 {
30 MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
31 txtUserName.Focus();
32 return;
33 }
34 if ((txtOldPassword.Text.Trim().Length == 0))
35 {
36 MessageBox.Show("Please enter old password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
37 txtOldPassword.Focus();
38 return;
39 }
40 if ((txtNewPassword.Text.Trim().Length == 0))
41 {
42 MessageBox.Show("Please enter new password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
43 txtNewPassword.Focus();
44 return;
45 }
46 if ((txtConfirmPassword.Text.Trim().Length == 0))
47 {
48 MessageBox.Show("Please confirm new password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
49 txtConfirmPassword.Focus();
50 return;
51 }
52 if ((txtNewPassword.TextLength < 5))
53 {
54 MessageBox.Show("The New Password Should be of Atleast 5 Characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
55 txtNewPassword.Text = "";
56 txtConfirmPassword.Text = "";
57 txtNewPassword.Focus();
58 return;
59 }
60 else if ((txtNewPassword.Text != txtConfirmPassword.Text))
61 {
62 MessageBox.Show("Password do not match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
63 txtNewPassword.Text = "";
64 txtOldPassword.Text = "";
65 txtConfirmPassword.Text = "";
66 txtOldPassword.Focus();
67 return;
68 }
69 else if ((txtOldPassword.Text == txtNewPassword.Text))
70 {
71 MessageBox.Show("Password is same..Re-enter new password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
72 txtNewPassword.Text = "";
73 txtConfirmPassword.Text = "";
74 txtNewPassword.Focus();
75 return;
76 }
77
78 con = new SqlConnection(cs.DBConn);
79 con.Open();
80 string co = "Update Registration set Password = '" + txtNewPassword.Text + "'where UserName='" + txtUserName.Text + "' and Password = '" + txtOldPassword.Text + "'";
81
82 cmd = new SqlCommand(co);
83 cmd.Connection = con;
84 RowsAffected = cmd.ExecuteNonQuery();
85 if ((RowsAffected > 0))
86 {
87 MessageBox.Show("Successfully changed", "Password", MessageBoxButtons.OK, MessageBoxIcon.Information);
88 this.Hide();
89 txtUserName.Text = "";
90 txtNewPassword.Text = "";
91 txtOldPassword.Text = "";
92 txtConfirmPassword.Text = "";
93 frmLogin LoginForm1 = new frmLogin();
94 LoginForm1.Show();
95 LoginForm1.txtUserName.Text = "";
96 LoginForm1.txtPassword.Text = "";
97 LoginForm1.ProgressBar1.Visible = false;
98 LoginForm1.txtUserName.Focus();
99 }
100 else
101 {
102 MessageBox.Show("invalid user name or password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
103 txtUserName.Text = "";
104 txtNewPassword.Text = "";
105 txtOldPassword.Text = "";
106 txtConfirmPassword.Text = "";
107 txtUserName.Focus();
108 }
109 if ((con.State == ConnectionState.Open))
110 {
111 con.Close();
112 }
113 con.Close();
114 }
115 catch (Exception ex)
116 {
117 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
118 }
119 }
120
121 private void ChangePassword_FormClosing(object sender, FormClosingEventArgs e)
122 {
123 this.Hide();
124 frmLogin frm = new frmLogin();
125 frm.txtUserName.Text = "";
126 frm.txtPassword.Text = "";
127 frm.ProgressBar1.Visible = false;
128 frm.txtUserName.Focus();
129 frm.Show();
130 }
131
132 private void frmChangePassword_Load(object sender, EventArgs e)
133 {
134
135 }
136
137 }
138 }